package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ygqh.baby.ao.JobStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgBmdism;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.service.YgBmdismService;
import com.ygqh.baby.shiro.utils.SessionUtil;

@Controller
@RequestMapping("/admin/bmdism")
public class BmdismController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private YgBmdismService ygBmdismService;

	@RequestMapping("edit")
	@ResponseBody
	public Message add(String bmdism, String remark) {
		AdminUser user = SessionUtil.getCurrentUser();
		YgBmdism ygBmdism = JSON.parseObject(bmdism, new TypeReference<YgBmdism>() {
		});
		if (ygBmdism.getId() != null) {
			ygBmdism.setUpdateTime(new Date());
			ygBmdism.setUpdateBy(user.getTrueName());
		} else {
			ygBmdism.setCreateBy(user.getTrueName());
			ygBmdism.setCreateTime(new Date());
		}

		return ygBmdismService.addBmdism(ygBmdism);
	}

	@RequestMapping("findById")
	@ResponseBody
	public Message findById(Long id) {
		YgBmdism bmdism = ygBmdismService.findById(id);
		return Message.success(bmdism);
	}

	@RequestMapping("getProductByBmdism")
	@ResponseBody
	public Message getProductByBmdism(QueryInfo queryInfo, Long id, String productName) {
		List<YgProduct> list = ygBmdismService.findProductByBmdism(queryInfo, id, productName);
		return Message.success(list);
	}

	@RequestMapping("search")
	@ResponseBody
	public Map<String, Object> search(String title, QueryInfo queryInfo, Date startDate, Date endDate, JobStatus jobStatus, String q) {
		ResultSet<YgBmdism> resultSet = ygBmdismService.search(queryInfo, title, startDate, endDate, jobStatus, q);
		return resultSet.toDatatables(null);
	}

	@RequestMapping("findList")
	@ResponseBody
	public Message findList(JobStatus jobStatus) {
		return Message.success(ygBmdismService.find(jobStatus));
	}

}
